<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="付款人标识" prop="userTagId">
        <el-input v-model="formData.userTagId" placeholder="请输入付款人标识" />
      </el-form-item>
      <el-form-item label="渠道商号" prop="huifuId">
        <el-input v-model="formData.huifuId" placeholder="请输入渠道商号" />
      </el-form-item>
      <el-form-item label="商户号" prop="sysId">
        <el-input v-model="formData.sysId" placeholder="请输入商户号" />
      </el-form-item>
      <el-form-item label="商户简称" prop="sysShortName">
        <el-input v-model="formData.sysShortName" placeholder="请输入商户简称" />
      </el-form-item>
      <el-form-item label="商户名称" prop="sysIdName">
        <el-input v-model="formData.sysIdName" placeholder="请输入商户名称" />
      </el-form-item>
      <el-form-item label="产品号" prop="productNumber">
        <el-input v-model="formData.productNumber" placeholder="请输入产品号" />
      </el-form-item>
      <el-form-item label="汇付终端id" prop="huiTerminalId">
        <el-input v-model="formData.huiTerminalId" placeholder="请输入汇付终端id" />
      </el-form-item>
      <el-form-item label="终端名称" prop="huiTerminalName">
        <el-input v-model="formData.huiTerminalName" placeholder="请输入终端名称" />
      </el-form-item>
      <el-form-item label="终端类型" prop="huiTerminalType">
        <el-select v-model="formData.huiTerminalType" placeholder="请选择终端类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="凭证号" prop="pzNumber">
        <el-input v-model="formData.pzNumber" placeholder="请输入凭证号" />
      </el-form-item>
      <el-form-item label="汇付订单号" prop="huifuOrderId">
        <el-input v-model="formData.huifuOrderId" placeholder="请输入汇付订单号" />
      </el-form-item>
      <el-form-item label="外部订单号" prop="outsideOrderId">
        <el-input v-model="formData.outsideOrderId" placeholder="请输入外部订单号" />
      </el-form-item>
      <el-form-item label="交易类型" prop="exchangeType">
        <el-select v-model="formData.exchangeType" placeholder="请选择交易类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="交易方式订单号" prop="exchangeWaySeq">
        <el-input v-model="formData.exchangeWaySeq" placeholder="请输入交易方式订单号" />
      </el-form-item>
      <el-form-item label="交易方式 微信支付宝等" prop="exchangeWay">
        <el-input v-model="formData.exchangeWay" placeholder="请输入交易方式 微信支付宝等" />
      </el-form-item>
      <el-form-item label="商品描述" prop="productRemarks">
        <el-input v-model="formData.productRemarks" placeholder="请输入商品描述" />
      </el-form-item>
      <el-form-item label="交易金额" prop="exchangeMoney">
        <el-input v-model="formData.exchangeMoney" placeholder="请输入交易金额" />
      </el-form-item>
      <el-form-item label="手续费" prop="handlingFee">
        <el-input v-model="formData.handlingFee" placeholder="请输入手续费" />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundAmount">
        <el-input v-model="formData.refundAmount" placeholder="请输入退款金额" />
      </el-form-item>
      <el-form-item label="交易状态" prop="payStatus">
        <el-radio-group v-model="formData.payStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="交易时间" prop="exchangeTime">
        <el-date-picker
          v-model="formData.exchangeTime"
          type="date"
          value-format="x"
          placeholder="选择交易时间"
        />
      </el-form-item>
      <el-form-item label="付款银行" prop="payBank">
        <el-input v-model="formData.payBank" placeholder="请输入付款银行" />
      </el-form-item>
      <el-form-item label="借贷标识" prop="loanTag">
        <el-input v-model="formData.loanTag" placeholder="请输入借贷标识" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { OrderApi, OrderVO } from '@/api/huifu/order'

/** 汇付对帐订单 表单 */
defineOptions({ name: 'OrderForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  userTagId: undefined,
  huifuId: undefined,
  sysId: undefined,
  sysShortName: undefined,
  sysIdName: undefined,
  productNumber: undefined,
  huiTerminalId: undefined,
  huiTerminalName: undefined,
  huiTerminalType: undefined,
  pzNumber: undefined,
  huifuOrderId: undefined,
  outsideOrderId: undefined,
  exchangeType: undefined,
  exchangeWaySeq: undefined,
  exchangeWay: undefined,
  productRemarks: undefined,
  exchangeMoney: undefined,
  handlingFee: undefined,
  refundAmount: undefined,
  payStatus: undefined,
  exchangeTime: undefined,
  payBank: undefined,
  loanTag: undefined,
})
const formRules = reactive({
  huifuId: [{ required: true, message: '渠道商号不能为空', trigger: 'blur' }],
  sysId: [{ required: true, message: '商户号不能为空', trigger: 'blur' }],
  sysIdName: [{ required: true, message: '商户名称不能为空', trigger: 'blur' }],
  huiTerminalId: [{ required: true, message: '汇付终端id不能为空', trigger: 'blur' }],
  huifuOrderId: [{ required: true, message: '汇付订单号不能为空', trigger: 'blur' }],
  exchangeType: [{ required: true, message: '交易类型不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OrderApi.getOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OrderVO
    if (formType.value === 'create') {
      await OrderApi.createOrder(data)
      message.success(t('common.createSuccess'))
    } else {
      await OrderApi.updateOrder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    userTagId: undefined,
    huifuId: undefined,
    sysId: undefined,
    sysShortName: undefined,
    sysIdName: undefined,
    productNumber: undefined,
    huiTerminalId: undefined,
    huiTerminalName: undefined,
    huiTerminalType: undefined,
    pzNumber: undefined,
    huifuOrderId: undefined,
    outsideOrderId: undefined,
    exchangeType: undefined,
    exchangeWaySeq: undefined,
    exchangeWay: undefined,
    productRemarks: undefined,
    exchangeMoney: undefined,
    handlingFee: undefined,
    refundAmount: undefined,
    payStatus: undefined,
    exchangeTime: undefined,
    payBank: undefined,
    loanTag: undefined,
  }
  formRef.value?.resetFields()
}
</script>